<template>
  <div class="reception-meal">
    <div class="header">
      <h2>餐饮记录管理</h2>
      <p>查看和管理老人的餐饮记录</p>
    </div>

    <!-- 搜索筛选区域 -->
    <div class="filter-container">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input
            v-model="searchParams.elderName"
            placeholder="老人姓名"
            clearable
            @clear="handleSearch"
            @keyup.enter="handleSearch"
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-select
            v-model="searchParams.mealType"
            placeholder="餐饮类型"
            clearable
            @change="handleSearch"
          >
            <el-option label="早餐" value="早餐" />
            <el-option label="午餐" value="午餐" />
            <el-option label="晚餐" value="晚餐" />
            <el-option label="加餐" value="加餐" />
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-date-picker
            v-model="searchParams.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="handleSearch"
          />
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-col>
      </el-row>
    </div>

    <!-- 数据表格 -->
    <div class="table-container">
      <el-table
        :data="tableData"
        v-loading="loading"
        border
        stripe
        style="width: 100%"
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="elderName" label="老人姓名" width="120" align="center" />
        <el-table-column prop="mealType" label="餐饮类型" width="100" align="center">
          <template #default="{ row }">
            <el-tag :type="getMealTypeTag(row.mealType)">
              {{ row.mealType }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="mealTime" label="用餐时间" width="180" align="center">
          <template #default="{ row }">
            {{ formatDateTime(row.mealTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="foodContent" label="食物内容" min-width="200" show-overflow-tooltip />
        <el-table-column prop="intakeAmount" label="摄入量" width="100" align="center">
          <template #default="{ row }">
            <el-tag :type="getIntakeTag(row.intakeAmount)">
              {{ row.intakeAmount }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="caregiverName" label="负责护工" width="120" align="center" />
        <el-table-column prop="specialDiet" label="特殊饮食" width="120" align="center">
          <template #default="{ row }">
            <span v-if="row.specialDiet">{{ row.specialDiet }}</span>
            <span v-else class="text-muted">无</span>
          </template>
        </el-table-column>
        <el-table-column prop="rating" label="评分" width="100" align="center">
          <template #default="{ row }">
            <el-rate
              v-model="row.rating"
              disabled
              show-score
              text-color="#ff9900"
              score-template="{value}分"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center" fixed="right">
          <template #default="{ row }">
            <el-button
              type="primary"
              link
              size="small"
              @click="handleViewDetail(row)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 详情对话框 -->
    <el-dialog
      v-model="detailDialogVisible"
      title="餐饮记录详情"
      width="600px"
      :close-on-click-modal="false"
    >
      <div v-if="currentRecord" class="detail-content">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="老人姓名">
            {{ currentRecord.elderName }}
          </el-descriptions-item>
          <el-descriptions-item label="餐饮类型">
            <el-tag :type="getMealTypeTag(currentRecord.mealType)">
              {{ currentRecord.mealType }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="用餐时间">
            {{ formatDateTime(currentRecord.mealTime) }}
          </el-descriptions-item>
          <el-descriptions-item label="摄入量">
            <el-tag :type="getIntakeTag(currentRecord.intakeAmount)">
              {{ currentRecord.intakeAmount }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="负责护工">
            {{ currentRecord.caregiverName }}
          </el-descriptions-item>
          <el-descriptions-item label="特殊饮食">
            {{ currentRecord.specialDiet || '无' }}
          </el-descriptions-item>
          <el-descriptions-item label="食物内容" :span="2">
            {{ currentRecord.foodContent }}
          </el-descriptions-item>
          <el-descriptions-item label="营养信息" :span="2">
            {{ currentRecord.nutritionInfo || '暂无营养信息' }}
          </el-descriptions-item>
          <el-descriptions-item label="反馈意见" :span="2">
            {{ currentRecord.feedback || '暂无反馈' }}
          </el-descriptions-item>
          <el-descriptions-item label="评分">
            <el-rate
              v-model="currentRecord.rating"
              disabled
              show-score
              text-color="#ff9900"
              score-template="{value}分"
            />
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <template #footer>
        <el-button @click="detailDialogVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { Search, Refresh } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

// 响应式数据
const loading = ref(false)
const tableData = ref([])
const detailDialogVisible = ref(false)
const currentRecord = ref(null)

// 搜索参数
const searchParams = reactive({
  elderName: '',
  mealType: '',
  dateRange: []
})

// 分页参数
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 模拟数据
const mockData = [
  {
    id: 1,
    elderName: '张大爷',
    mealType: '早餐',
    mealTime: '2024-10-04 07:30:00',
    foodContent: '小米粥、鸡蛋、馒头、咸菜',
    intakeAmount: '正常',
    caregiverName: '张护工',
    specialDiet: '低盐',
    nutritionInfo: '富含蛋白质和碳水化合物',
    feedback: '老人食欲良好',
    rating: 4
  },
  {
    id: 2,
    elderName: '李大妈',
    mealType: '早餐',
    mealTime: '2024-10-04 07:45:00',
    foodContent: '燕麦粥、牛奶、面包、水果',
    intakeAmount: '较少',
    caregiverName: '李护工',
    specialDiet: '糖尿病饮食',
    nutritionInfo: '低糖高纤维',
    feedback: '老人今天食欲不佳',
    rating: 3
  },
  {
    id: 3,
    elderName: '王爷爷',
    mealType: '午餐',
    mealTime: '2024-10-04 12:00:00',
    foodContent: '米饭、清蒸鱼、炒青菜、豆腐汤',
    intakeAmount: '正常',
    caregiverName: '王护工',
    specialDiet: '低脂',
    nutritionInfo: '均衡营养',
    feedback: '老人喜欢今天的菜品',
    rating: 5
  },
  {
    id: 4,
    elderName: '赵奶奶',
    mealType: '午餐',
    mealTime: '2024-10-04 12:15:00',
    foodContent: '米饭、炖鸡汤、炒蔬菜',
    intakeAmount: '正常',
    caregiverName: '赵护工',
    specialDiet: '软食',
    nutritionInfo: '易消化',
    feedback: '老人吃得很好',
    rating: 4
  },
  {
    id: 5,
    elderName: '刘大爷',
    mealType: '晚餐',
    mealTime: '2024-10-04 18:00:00',
    foodContent: '面条、炒肉丝、汤',
    intakeAmount: '较多',
    caregiverName: '刘护工',
    specialDiet: '',
    nutritionInfo: '碳水化合物充足',
    feedback: '老人晚餐食欲很好',
    rating: 4
  }
]

// 生命周期
onMounted(() => {
  loadTableData()
})

// 加载表格数据
const loadTableData = async () => {
  loading.value = true
  try {
    // 模拟API调用
    await new Promise(resolve => setTimeout(resolve, 500))
    
    // 应用搜索过滤
    let filteredData = [...mockData]
    
    if (searchParams.elderName) {
      filteredData = filteredData.filter(item => 
        item.elderName.includes(searchParams.elderName)
      )
    }
    
    if (searchParams.mealType) {
      filteredData = filteredData.filter(item => 
        item.mealType === searchParams.mealType
      )
    }
    
    // 应用分页
    const start = (pagination.currentPage - 1) * pagination.pageSize
    const end = start + pagination.pageSize
    tableData.value = filteredData.slice(start, end)
    pagination.total = filteredData.length
    
  } catch (error) {
    ElMessage.error('加载数据失败')
    console.error('加载数据失败:', error)
  } finally {
    loading.value = false
  }
}

// 搜索处理
const handleSearch = () => {
  pagination.currentPage = 1
  loadTableData()
}

// 重置搜索
const handleReset = () => {
  Object.assign(searchParams, {
    elderName: '',
    mealType: '',
    dateRange: []
  })
  pagination.currentPage = 1
  loadTableData()
}

// 分页大小改变
const handleSizeChange = (size) => {
  pagination.pageSize = size
  pagination.currentPage = 1
  loadTableData()
}

// 当前页改变
const handleCurrentChange = (page) => {
  pagination.currentPage = page
  loadTableData()
}

// 查看详情
const handleViewDetail = (record) => {
  currentRecord.value = record
  detailDialogVisible.value = true
}

// 工具函数
const getMealTypeTag = (type) => {
  const typeMap = {
    '早餐': 'success',
    '午餐': 'warning',
    '晚餐': 'info',
    '加餐': 'primary'
  }
  return typeMap[type] || 'default'
}

const getIntakeTag = (amount) => {
  const amountMap = {
    '较少': 'warning',
    '正常': 'success',
    '较多': 'info'
  }
  return amountMap[amount] || 'default'
}

const formatDateTime = (dateTime) => {
  if (!dateTime) return ''
  return new Date(dateTime).toLocaleString('zh-CN')
}
</script>

<style scoped>
.reception-meal {
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  min-height: calc(100vh - 140px);
}

.header {
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #e8e8e8;
}

.header h2 {
  margin: 0;
  color: #303133;
  font-size: 24px;
}

.header p {
  margin: 5px 0 0;
  color: #909399;
  font-size: 14px;
}

.filter-container {
  margin-bottom: 20px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 6px;
}

.table-container {
  margin-bottom: 20px;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.detail-content {
  line-height: 1.6;
}

.text-muted {
  color: #909399;
  font-style: italic;
}

:deep(.el-descriptions__label) {
  font-weight: 600;
  width: 100px;
}

:deep(.el-descriptions__content) {
  color: #606266;
}
</style>
